var setting = {
    data: {
        simpleData: {
            enable: true,
            idKey: "id",
            pIdKey: "parentId",
            rootPId: 0
        },
        key: {
            url:"nourl"
        }
    }
};
var ztree;

var vm = new Vue({
    el:'#rrapp',
    data:{
        showList: true,
        title: null,
        office:{
            parentName:null,
            parentId:0,
            sort:1
        }
    },
    methods: {
        getOffice: function(parentId){
            //加载部门树
            

            //加载部门树
            $.ajax({
                type: "GET",
                url: "/office/selectOfficeZTreeListChooseOfficeId?userId=",
                dataType: "json",
                success: function(r){
                    if(r.status.statusCode === 0){
                        ztree = $.fn.zTree.init($("#officeTree"), setting, r.result);
                        if(parentId != null ){
                            var node = ztree.getNodeByParam("id", vm.office.parentId);
                            ztree.selectNode(node);
                        }

                    }else{
                        alert(r.status.statusReason);
                    }
                }
            });
            
        },
        add: function(){
            vm.showList = false;
            vm.title = "新增";
            vm.office = {parentName:null,parentId:0,sort:1};
            vm.getOffice(null);
        },
        update: function () {

            var officeId = getOfficeId();
            if(officeId == null){
                return false;
            }
            vm.showList = false;
            vm.title = "编辑";
            $.ajax({
                type: "GET",
                url: "/office/info?id="+officeId,
                dataType: "json",
                success: function(r){
                    if(r.status.statusCode == 0){
                        vm.office = r.result;
                        vm.getOffice(vm.office.parentId);
                    }else{
                        alert(r.status.statusReason);
                    }
                },
                error:function(r){
                    confirm('由于长时间未操作，确定要重新登陆吗？', function(){
                        window.location.href = "/views/login.html" ;
                    });
                }
            });
        },
        del: function () {
            var officeId = getOfficeId();
            if(officeId == null){
                return ;
            }


            confirm('确定要删除选中的记录？', function(){
                var dataJson =  {"id": officeId} ;
                $.ajax({
                    type: "POST",
                    url: "/office/delete",
                    contentType: "application/json",
                    data: JSON.stringify(dataJson),
                    dataType: "json",
                    success: function(r){
                        if(r.status.statusCode == 0){
                            alert('操作成功', function(){
                                vm.reload();
                            });
                        }else{
                            alert(r.status.statusReason);
                        }
                    },
                    error:function(r){
                        confirm('由于长时间未操作，确定要重新登陆吗？', function(){
                            window.location.href = "/views/login.html" ;
                        });
                    }
                });
            });
        },
        saveOrUpdate: function (event) {
            $.ajax({
                type: "POST",
                url: "/office/saveOrUpdate",
                contentType: "application/json",
                data: JSON.stringify(vm.office),
                success: function(r){
                    if(r.status.statusCode === 0){
                        alert('操作成功', function(){
                            vm.reload();
                        });
                    }else{
                        alert(r.status.statusReason);
                    }
                },
                error:function(r){
                    confirm('由于长时间未操作，确定要重新登陆吗？', function(){
                        window.location.href = "/views/login.html" ;
                    });
                }
            });
        },
        officeTree: function(){
            layer.open({
                type: 1,
                offset: '50px',
                skin: 'layui-layer-molv',
                title: "选择部门",
                area: ['300px', '450px'],
                shade: 0,
                shadeClose: false,
                content: jQuery("#officeLayer"),
                btn: ['确定', '取消'],
                btn1: function (index) {
                    var node = ztree.getSelectedNodes();
                    //选择上级部门
                    vm.office.parentId = node[0].id;
                    vm.office.parentName = node[0].name;

                    layer.close(index);
                }
            });
        },
        reload: function () {
            vm.showList = true;
            Office.table.refresh();
        }
    }
});

var Office = {
    id: "officeTable",
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Office.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},
        {title: '部门ID', field: 'id', visible: false, align: 'center', valign: 'middle', width: '80px'},
        {title: '部门名称', field: 'name', align: 'center', valign: 'middle', sortable: true, width: '180px'},
        {title: '排序号', field: 'sort', align: 'center', valign: 'middle', sortable: true, width: '100px'}]
    return columns;
};


function getOfficeId () {
    var selected = $('#officeTable').bootstrapTreeTable('getSelections');
    if (selected.length == 0) {
        alert("请选择一条记录");
        return null;
    } else {
        return selected[0].id;
    }
}


$(function () {
        var colunms = Office.initColumn();
        var table = new TreeTable(Office.id, "../../../../office/list", colunms);
        table.setExpandColumn(2);
        table.setIdField("id");
        table.setCodeField("id");
        table.setParentCodeField("parentId");
        table.setExpandAll(false);
        table.init();
        Office.table = table;
});
